package org.swxjava.assembler.avm1;

import org.swxjava.assembler.ObjectCompiler;

public class AsArrayElement {

	private AsArrayElement nextElement;
	private int index;
	private ObjectCompiler value;

	public AsArrayElement(int index, ObjectCompiler value) {
		this.index = index;
		this.value = value;
	}

	public void setNext(AsArrayElement nextElement) {
		this.nextElement = nextElement;
	}
	
	public AsArrayElement getNext() {
		return nextElement;
	}

	public int index() {
		return index;
	}
	
	public ObjectCompiler value() {
		return value;
	}
	
	public boolean isComplex() {
		boolean complex = false;
		complex = (value instanceof AsArray || value instanceof AsObject);
		return complex;
	}
	
	public boolean isNew() {
		boolean isNew = false;
		isNew = (value instanceof AsClass);
		return isNew;
	}

	public int getByteSize() {
		int size = value.getByteSize();
		if(!this.isNew()) {
			if (nextElement == null) {
				size += 5; // INT int(4) ArrayLength
			} else if (!nextElement.isComplex()) {
				// Stop calculating next byte size.
				size += nextElement.getByteSize();
			}
		}
		return size;
	}
}
